// 引用使用es6的module引入和定义
// 全局变量以g_开头
// 私有函数以_开头

import { Config } from 'config.js';

class Token {
    constructor() {
      this.verifyUrl = Config.restUrl + 'xcx-member/check-token';
      this.tokenUrl = Config.restUrl + 'xcx-member/xcx-login';
    }

    verify() {
        var token = wx.getStorageSync('token');
        if (!token) {
            this.getTokenFromServer();
        }
        else {
            this.veirfyFromServer(token);
        } 
    }

    veirfyFromServer(token) {
        var that = this;
        wx.request({
          url: that.verifyUrl + '?token=' + wx.getStorageSync('token'),
            method: 'Get',
            data: {},
            success: function (res) {
                var status = res.data.status;
                if(!status){
                    that.getTokenFromServer();
                }
            }
        })
    }

    getTokenFromServer(callBack) {
        var that  = this;
        wx.login({
            success: function (res) {
              if(res.code){
                if (wx.getStorageSync('loging')) {
                  return false;
                }
                wx.setStorageSync('loging', true);
                wx.request({
                  url: that.tokenUrl,
                  method: 'POST',
                  data: {code: res.code },
                  success: function (res) {
                    wx.setStorageSync('loging', false);
                    if (res.data.user.status == 0) {
                      wx.redirectTo({
                        url: '/pages/home/forbidden',
                      })
                      return false;
                    }
                    wx.setStorageSync('token', res.data.token);
                    wx.setStorageSync('uid', res.data.user.id);
                    wx.setStorageSync('userInfo', res.data.user);
                    callBack && callBack(res.data.token);
                  }
                })
              }
              else{
                console.log('登录失败！' + res.errMsg)
              }

          },
          fail: function () {
            wx.hideLoading();
            wx.showToast({
              title: '网络异常，请重试',
              icon: 'none',
              duration: 3000
            })
          },
          complete: function (res) {
            wx.setStorageSync('loging', false);
          }
        })
        wx.getSetting({
          success: function (res) {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getUserInfo 获取头像昵称
              wx.getUserInfo({
                success: function (res) {
                  wx.setStorageSync('userInfo', res.userInfo);
                  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                  // 所以此处加入 callback 以防止这种情况
                  // if (typeof (this.userInfoReadyCallback) != 'undefined' && this.userInfoReadyCallback) {
                  //   this.userInfoReadyCallback(res)
                  // }
                }
              })
            }
          }
        })
        
    }
}

export {Token};